﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Entity;

namespace School.SystemManage
{
    public partial class left : Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        IQueryable<AdimMenu> data = null;

        public void Add()
        {
            Response.Write("left");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //隐藏全部菜单，仅用来测试。
            allMenu.Visible = false;
          

            data = db.AdimMenu;

            ltrMenu.Text= GetMenu();
        }

        private string  GetMenu()
        {
            
            int roleid = int.Parse(Security.Cryptography.DESDecrypt(GetCookie("UserRoleID"), "KYLECHEN"));
            string permissions = db.Role.FirstOrDefault(r => r.RID == roleid).Permissions;
            var parentMenu = db.AdimMenu.Where("it.mid in {" + permissions + "}");

            StringBuilder menuHtml = new StringBuilder();
            menuHtml.Append("<ul id='browser' class='filetree'>");

            foreach (var parentMenuItem in parentMenu)
            {
                menuHtml.Append("<li>");
                menuHtml.AppendFormat("<span class='folder'>{0}</span>", parentMenuItem.name);
                menuHtml.Append(GetSubMenu(parentMenuItem.mid));
                menuHtml.Append("</li>");
            }

            menuHtml.Append("</ul>");

            return menuHtml.ToString();
        }

        private string GetSubMenu(int parentMenuID)
        {
            var parentMenu = db.AdimMenu.FirstOrDefault(a => a.mid == parentMenuID);
            if (!string.IsNullOrEmpty(parentMenu.childrenId))
            {
                string subItems = parentMenu.childrenId;
                var subMenus = db.AdimMenu.Where("it.mid in {" + subItems + "}");

                StringBuilder subMenuHtml = new StringBuilder();
                subMenuHtml.Append("<ul>");

                foreach (var subMenuItem in subMenus)
                {
                    subMenuHtml.Append("<li>");
                    if (string.IsNullOrEmpty(subMenuItem.childrenId) && !string.IsNullOrEmpty(subMenuItem.linkurl))
                    {
                        subMenuHtml.AppendFormat("<span class='file'><a href='{0}' target='rightFrame'>{1}</a></span>", subMenuItem.linkurl, subMenuItem.name);
                    }
                    else
                    {
                        subMenuHtml.AppendFormat("<span class='folder'>{0}</span>", subMenuItem.name);
                    }
                    subMenuHtml.Append(GetSubMenu(subMenuItem.mid));
                    subMenuHtml.Append("</li>");
                }

                subMenuHtml.Append("</ul>");

                return subMenuHtml.ToString();
            }

            return string.Empty;
        }
    }
}
